package com.rocksea.app.db.StaticLoad;

import android.util.Base64;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.litesuits.orm.db.annotation.Default;
import com.litesuits.orm.db.annotation.Ignore;
import com.litesuits.orm.db.annotation.Mapping;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
import com.litesuits.orm.db.enums.Relation;

import com.rocksea.app.Account;
import com.rocksea.app.entry.JYData;
import com.rocksea.app.http.HttpUtil;
import com.rocksea.app.utils.PerUtils;

import org.json.JSONException;


import java.util.ArrayList;

/**
 * Created by Mrw on 2017/8/5.
 *
 */
@Table("StaticLoad")
public class StaticLoad {
    @PrimaryKey(AssignType.AUTO_INCREMENT)
    private int id;
    @Ignore
    public String randomCode;
    @Ignore
    public String account;
    public long inspectionFormId;
    public String inspectionFormNum;

    public long pileId;

    @Default("RS")
    public String vendorId;
    public int remoteId;
    public String baseInfoId;
    public int testType;
    public String standard = "";
    public String machineId;
    public String serialNo;
    public String pileNo;
    public String fileName;
    public int preSeries;
    public double maxLoad;
    public double boardArea;
    public String startTime;
    public int[] loadInterva = new int[16];
    public int[] unloadInterval = new int[16];
    public String sensorID;
    public int[] sensorUsed = new int[16];
    public int pressType;
    public String pressID;
    public double fSensorAdjust;
    public double fSensorMax;
    public String customParam;
    public int validDataCount;
    public int validLogCount;
    public String coordinateX;
    public String coordinateY;
    public byte[] sourceData;
    public byte machineType;

    public byte isTesting = 0; // 0 -ing 1-


    @Default("-1")
    public int serviceId;

    @Default("0")
    public byte isLoad;
    @Default("0")
    public long upLoadTime;
    public long creatTime;

    public byte GpsIsValid;
    public double GpsLongitude;
    public double GpsLatitude;

    @Mapping(Relation.OneToMany)
    public ArrayList<StaticLoad.StaticLoadLog> mStaticLoadLog;

    @Mapping(Relation.OneToMany)
    public ArrayList<StaticLoadTest> mStaticLoadTest;

    public StaticLoad() {
    }

    public StaticLoad(String json)
    {
        JYData mData = JSON.parseObject(json,JYData.class);
        pileId = mData.pileId;
        inspectionFormId = mData.inspectionFormId;
        inspectionFormNum = mData.inspectionFormNum;

        vendorId = mData.VendorID;
        baseInfoId = mData.BaseInfoId;
        testType = mData.TestType;
        standard = mData.StandardString;
        machineId = mData.MachineID;
        serialNo = mData.SerialNo;
        pileNo = mData.PileNo;
        fileName = mData.FileName;
        preSeries = mData.PreSeries;
        maxLoad = mData.MaxLoad;
        boardArea = mData.BoardArea;
        startTime = mData.StartTime;
        for (int i = 0; i < mData.LoadInterva.length; i++)
            loadInterva[i] = mData.LoadInterva[i];
        for (int i = 0; i < mData.UnloadInterval.length; i++)
            unloadInterval[i] = mData.UnloadInterval[i];
        sensorID = mData.SensorID;
        System.arraycopy(mData.SensorUsed, 0, sensorUsed, 0, mData.SensorUsed.length);
        pressType = mData.PressType;
        fSensorAdjust = mData.FSensorAdjust;
        fSensorMax = mData.FSensorMax;
        pressID = mData.PressID;
        customParam = mData.CustomParam;
        //上传的时候赋值
        validDataCount = 0;
        validLogCount = 0;
        coordinateX = HttpUtil.encyptDES(String.valueOf(mData.GpsLongitude));
        coordinateY = HttpUtil.encyptDES(String.valueOf(mData.GpsLongitude));
        GpsIsValid = mData.GpsIsValid;
        GpsLatitude = mData.GpsLatitude;
        GpsLongitude = mData.GpsLongitude;
        remoteId = mData.remoteId;
        if (mData.sourceData!=null)
            sourceData = Base64.decode(mData.sourceData,0);
    }

    @Table("StaticLoadTest")
    public static class StaticLoadTest
    {
        @PrimaryKey(AssignType.AUTO_INCREMENT)
        private int id;
        @Ignore
        public String randomCode;
        @Ignore
        public String account;
        @Ignore
        public long staticLoadId;

        public int remoteId;
        public String baseInfoId;
        public int curValidData;
        public String uuid;
        public String sampleTime;
        public int loadDirect;
        public int grade;
        public int sampleCount;
        public int timeCount;
        public double loading;
        public double realLoading;
        public double realPress;
        public float[] sensorValue;
        public double saverage;
        @Default("0")
        public  byte isLoad;
        @Default("0")
        public long upLoadTime;
        public long creatTime;

        public StaticLoadTest() {
        }

        public StaticLoadTest(String json)
        {
            JYData.Data mData = JSON.parseObject(json,JYData.Data.class);

            baseInfoId = mData.BaseInfoId;
            uuid = mData.uuid;
            sampleTime = mData.SampleTime;
            loadDirect = mData.LoadDirect;
            grade = mData.Grade;
            sampleCount = mData.SampleCount;
            timeCount = mData.TimeCount;
            loading = mData.Loading;
            realLoading = PerUtils.getTwoData(mData.RealLoading);
            realPress = PerUtils.getTwoData(mData.RealPress);
            sensorValue = mData.SensorValue;
            saverage = mData.Saverage;
            remoteId = mData.remoteId;
        }
    }


    @Table("StaticLoadLog")
    public static class StaticLoadLog {
        @PrimaryKey(AssignType.AUTO_INCREMENT)
        private  int id;
        @Ignore
        public String randomCode;
        @Ignore
        public String account;
        @Ignore
        public long staticLoadId;

        public int remoteId;
        public int curValidData;
        public String baseInfoId;
        public String uuid;
        public String eventTime;
        public String eventInfo;
        public String remark;
        @Default("0")
        public int isLoad;
        @Default("0")
        public long upLoadTime;
        public long creatTime;
        public StaticLoadLog()
        {
        }
        public StaticLoadLog(String json)
        {
            JYData.Log log = JSON.parseObject(json,JYData.Log.class);
            uuid = log.uuid;
            eventTime = log.EventTime;
            eventInfo = log.EventInfo;
            remark = log.Remark;
            remoteId = log.remoteId;
            baseInfoId = log.BaseInfoId;
        }
    }


    public com.mor.dataacquisition.vo.staticLoad.StaticLoad getBJYKStaticLoad(int inspectionFormId,String inspectionFormNum,long pileId)
    {
        com.mor.dataacquisition.vo.staticLoad.StaticLoad staticLoad
                = new com.mor.dataacquisition.vo.staticLoad.StaticLoad();

        staticLoad.randomCode = Account.getRandomCode();
        staticLoad.account = Account.getAccount();
        staticLoad.inspectionFormId = inspectionFormId;
        staticLoad.inspectionFormNum = inspectionFormNum;
        staticLoad. pileId = pileId+"";

        staticLoad. vendorId = vendorId;
        staticLoad. testType = testType;
        if (standard != null)
        {
            if (standard.contains("压"))
            {
                staticLoad.standard = 0;
            }
            else if (standard.contains("拔"))
            {
                staticLoad.standard = 1;
            }
            else if (standard.contains("速"))
            {
                staticLoad.standard = 2;
            }
            else if (standard.contains("衡"))
            {
                staticLoad.standard = 3;
            }
            else if (standard.contains("复"))
            {
                staticLoad.standard = 4;
            }
            else if (standard.contains("循"))
            {
                staticLoad.standard = 5;
            }
            else
            {
                staticLoad.standard = 0;
            }
        }
        staticLoad. machineId = machineId;
        staticLoad. serialNo = "RSJZ";
        staticLoad. pileNo = pileNo;
        staticLoad. fileName = fileName;
        staticLoad. preSeries = preSeries;
        staticLoad. maxLoad = maxLoad;

        if (boardArea < 0)
            staticLoad. boardArea = 1f;
        else
            staticLoad. boardArea = boardArea;

        staticLoad. startTime = startTime;
        staticLoad. loadInterva = loadInterva;
        staticLoad. unloadInterval = unloadInterval;
        staticLoad. sensorId = sensorID;
        staticLoad. sensorUsed = sensorUsed;
        staticLoad. pressType = pressType;
        staticLoad. pressId = pressID;
        double formatAdjust = PerUtils.getThrerData(fSensorAdjust);
        if (formatAdjust > 100)
            staticLoad. fSensorAdjust = 88.520f;
        else
            staticLoad. fSensorAdjust = fSensorAdjust;
        staticLoad. fSensorMax = fSensorMax;
        staticLoad. customParam = customParam;
        staticLoad. validDataCount = validDataCount;
        staticLoad. validLogCount = validLogCount;
        staticLoad. coordinateX = coordinateX;
        staticLoad. coordinateY = coordinateY;
        return staticLoad;
    }

    
    public  com.mor.dataacquisition.vo.staticLoad.StaticLoadTest getBJTKStaticLoadTest(long staticLoadId,StaticLoadTest mData,int curValidData)
    {
        com.mor.dataacquisition.vo.staticLoad.StaticLoadTest staticLoadTest =
                new com.mor.dataacquisition.vo.staticLoad.StaticLoadTest();
        staticLoadTest. randomCode = Account.getRandomCode();
        staticLoadTest. account = Account.getAccount();
        staticLoadTest. staticLoadId = staticLoadId;
        staticLoadTest. curValidData = curValidData;
        staticLoadTest. uuid = mData.uuid.replace("-","");
        staticLoadTest. sampleTime = mData.sampleTime;
        staticLoadTest. loadDirect = mData.loadDirect;
        staticLoadTest. grade = mData.grade;
        staticLoadTest. sampleCount = mData.sampleCount;
        staticLoadTest. timeCount = mData.timeCount;
        staticLoadTest. loading = PerUtils.getTwoData(mData.loading);
        staticLoadTest. realLoading = PerUtils.getTwoData(mData.realLoading);
        staticLoadTest. realPress = PerUtils.getTwoData(mData.realPress);

        double[] sensorValue1 = new double[16];
        for (int i = 0; i < 16; i++)
        {
            if (i >= mData.sensorValue.length)
                sensorValue1[i] = -8888;
            else
            {
                if (mData.sensorValue[i] < 100 && mData.sensorValue[i] > -100)
                    sensorValue1[i] = PerUtils.getTwoData((double)mData.sensorValue[i]);
                else
                    sensorValue1[i] = -8888;
            }
        }
        staticLoadTest. sensorValue = sensorValue1;
        staticLoadTest. saverage = PerUtils.getTwoData(mData.saverage);

        return staticLoadTest;

    }

    public com.mor.dataacquisition.vo.staticLoad.StaticLoadLog getBJTJStaticLoadLog(long staticLoadId,StaticLoadLog mData,int logIndex)
    {
        com.mor.dataacquisition.vo.staticLoad.StaticLoadLog staticLoadLog =
                new com.mor.dataacquisition.vo.staticLoad.StaticLoadLog();
        staticLoadLog. randomCode = Account.getRandomCode();
        staticLoadLog. account = Account.getAccount();
        staticLoadLog. staticLoadId = staticLoadId;
        staticLoadLog. curValidData = logIndex;
        staticLoadLog. uuid = mData.uuid.replace("-","");
        staticLoadLog. eventTime = mData.eventTime;
        staticLoadLog. eventInfo = mData.eventInfo;
        staticLoadLog. remark = mData.remark;
        return staticLoadLog;
    }
}
